package com.sfexpress.mycatweb.controller.monitor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sfexpress.mycatweb.controller.AppResult;
import com.sfexpress.mycatweb.entity.monitor.BasicMonitorInfo;
import com.sfexpress.mycatweb.entity.monitor.JvmCpuStat;
import com.sfexpress.mycatweb.entity.monitor.JvmHeapStat;
import com.sfexpress.mycatweb.entity.monitor.JvmMonitorInfo;
import com.sfexpress.mycatweb.service.monitor.BasicMonitorService;
import com.sfexpress.mycatweb.service.monitor.JvmMonitorService;

/**
 * 监控信息Controller
 * @author CrazyPig
 *
 */
@Controller
@RequestMapping("/monitor")
public class MonitorController {
	
	@Autowired
	private BasicMonitorService basicMonitorService;
	@Autowired
	private JvmMonitorService jvmMonitorService;

	/**
	 * 获取机器基本监控信息
	 * @param host
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/basicinfo")
	@ResponseBody
	public AppResult getMachineBasicMonitorInfo(@RequestParam String host) throws Exception {
		BasicMonitorInfo basicInfo = basicMonitorService.getBasicMonitorInfo(host);
		AppResult result = new AppResult();
		result.setSuccess(true);
		result.setRow(basicInfo);
		return result;
	}
	
	/**
	 * 获取机器CPU监控信息
	 * @param host
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/basicinfo/cpustat")
	@ResponseBody
	public AppResult getCpuStat(@RequestParam String host) throws Exception {
		BasicMonitorInfo basicInfo = basicMonitorService.getBasicMonitorInfo(host);
		AppResult result = new AppResult();
		result.setSuccess(true);
		result.setRow(basicInfo.getCpuInfo());
		return result;
	}
	
	/**
	 * 获取机器Memory监控信息
	 * @param host
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/basicinfo/memstat")
	@ResponseBody
	public AppResult getMemStat(@RequestParam String host) throws Exception {
		BasicMonitorInfo basicInfo = basicMonitorService.getBasicMonitorInfo(host);
		AppResult result = new AppResult();
		result.setSuccess(true);
		result.setRow(basicInfo.getMemInfo());
		return result;
	}
	
	/**
	 * 获取机器网络IO监控信息
	 * @param host
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/basicinfo/netstat")
	@ResponseBody
	public AppResult getNetStat(@RequestParam String host) throws Exception {
		BasicMonitorInfo basicInfo = basicMonitorService.getBasicMonitorInfo(host);
		AppResult result = new AppResult();
		result.setSuccess(true);
		result.setRow(basicInfo.getNetInfo());
		return result;
	}
	
	/**
	 * 获取java进程jvm监控信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/jvminfo")
	@ResponseBody
	public AppResult getJvmMonitorInfo() throws Exception {
		JvmMonitorInfo jvmInfo = jvmMonitorService.getJvmMonitorInfo();
		AppResult result = new AppResult();
		result.setSuccess(true);
		result.setRow(jvmInfo);
		return result;
	}
	
	/**
	 * 获取java进程cpu监控信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/jvminfo/cpustat")
	@ResponseBody
	public AppResult getJvmCpuStat() throws Exception {
		JvmMonitorInfo jvmInfo = jvmMonitorService.getJvmMonitorInfo();
		JvmCpuStat jvmCpuStat = jvmInfo.getCpuStat();
		AppResult result = new AppResult();
		result.setSuccess(true);
		result.setRow(jvmCpuStat);
		return result;
	}
	
	/**
	 * 获取java进程堆内存监控信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/jvminfo/heapstat")
	@ResponseBody
	public AppResult getJvmHeapStat() throws Exception {
		JvmMonitorInfo jvmInfo = jvmMonitorService.getJvmMonitorInfo();
		JvmHeapStat jvmHeapStat = jvmInfo.getHeapStat();
		AppResult result = new AppResult();
		result.setSuccess(true);
		result.setRow(jvmHeapStat);
		return result;
	}
	
	
	
}
